//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "UsersForm.h"
#include "DataModule.h"
#include "Access_class.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFUsers *FUsers;
//---------------------------------------------------------------------------
__fastcall TFUsers::TFUsers(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFUsers::FormCreate(TObject *Sender)
{
 Disp = new TTabCtrl();
 Disp->Add(lb, NULL, Edit1, NULL, BitBtn1, BitBtn1, NULL);
 Disp->Add(BitBtn1, NULL, BitBtn2, lb, NULL, Edit1, lb);
 Disp->Add(Edit1, lb, Edit2, NULL, BitBtn2, Edit2, BitBtn1);
 Disp->Add(Edit2, Edit1, BitBtn3, NULL, BitBtn2, BitBtn2, Edit1);
 Disp->Add(BitBtn2, BitBtn1, BitBtn3,Edit2, NULL, BitBtn3, Edit2);
 Disp->Add(BitBtn3, BitBtn2, NULL, NULL, NULL, NULL, BitBtn2); 
 ReadData();
}
//---------------------------------------------------------------------------


void __fastcall TFUsers::ReadData()
{
        //TODO: Add your source code here
        Ask->SQL->Text = "select * from PASS";
        Ask->Open();
        lb->Items->Clear();
        while(!Ask->Eof)
        {
         lb->Items->AddObject(Ask->FieldByName("NAME")->AsString, (TObject*)(Ask->FieldByName("IDPASS")->AsInteger));
         Ask->Next();
        }
        Ask->Close();
}
void __fastcall TFUsers::BitBtn2Click(TObject *Sender)
{
 //
 String HESH = AccessProvider::CodeMD5(Edit2->Text);
 Ask->SQL->Text = "insert into PASS(NAME, HESH, USERROLE) values ('"+Edit1->Text+"', '"+HESH+"', 2)";
 Ask->ExecSQL();
 ReadData();        
}
//---------------------------------------------------------------------------
void __fastcall TFUsers::BitBtn1Click(TObject *Sender)
{
 //
 if(lb->ItemIndex <0)return;
 int ID = (int)(lb->Items->Objects[lb->ItemIndex]);
 Ask->SQL->Text = "delete from PASS where IDPASS = " + String(ID);
 Ask->ExecSQL();
 ReadData();        
}
//---------------------------------------------------------------------------
void __fastcall TFUsers::ApplicationEvents1Message(tagMSG &Msg,
      bool &Handled)
{
 if(Msg.message == WM_KEYUP)
 {
  if(Msg.wParam == 27)
  {
   Handled = true;
   Close();
   return;
  }
 }
 Disp->Process(this, Msg, Handled);        
}
//---------------------------------------------------------------------------

void __fastcall TFUsers::FormClose(TObject *Sender, TCloseAction &Action)
{
 delete Disp;         
}
//---------------------------------------------------------------------------

